package controllers;

import models.Collection;
import models.CollectionGroup;
import service.CollectionInterface;
import service.impl.CollectionImpl;
import utils.JsonUtil;
import java.util.*;

/**
 * 收藏、分组action
 */
public class CollectionAction extends Application {

    //创建分组
    public static void createCollectionGroup(String params){
        CollectionGroup group = (CollectionGroup) JsonUtil.readJson2Entity(params, CollectionGroup.class);
        CollectionInterface collectionInterface = new CollectionImpl();
        collectionInterface.createCollectionGroup(group);
    }

    //创建收藏
    public static void collection(String params){
        Collection collection = (Collection) JsonUtil.readJson2Entity(params, Collection.class);
        CollectionInterface collectionInterface = new CollectionImpl();
        collectionInterface.collection(collection);
    }

    //获取收藏列表
    public static void getCollectionListByPage(String params) throws Exception {
        Map<String, Object> paramsMap = JsonUtil.paseJson2Map(params);
        Integer userId = (Integer) paramsMap.get("userId");
        Integer groupId = (Integer) paramsMap.get("groupId");
        CollectionInterface collectionInterface = new CollectionImpl();
        List<Collection> list = collectionInterface.getCollectionListByPage(userId, groupId);
        renderText(JsonUtil.parseObject(list));
    }

    //获取分组的列表
    public static void getCollectionGroupListByPage(String params) throws Exception {
        Map<String, Object> paramsMap = JsonUtil.paseJson2Map(params);
        Integer userId = (Integer) paramsMap.get("userId");
        CollectionInterface collectionInterface = new CollectionImpl();
        List<CollectionGroup> list = collectionInterface.getCollectionGroupListByPage(userId);
        renderText(JsonUtil.parseObject(list));
    }
}
